/**
File Name 	: DateTime.c
Name, NIM	: Muqtafi Akhmad, 23512021
 **/

#include <stdlib.h>

#include "DateTime.h"

/**
 * Get date from the given date time
 * @param DT date time
 * @return date
 */
Date GetDate(DateTime DT) {
    return DT.D;
}

/**
 * Get time from the given date time
 * @param DT date time
 * @param T time
 */
Time GetTime(DateTime DT) {
    return DT.T;
}

/**
 * Set the date of a given date time
 * @param DT date time
 * @param D date 
 */
void SetDate(DateTime *DT, Date D) {
    SetDay(GetDay(D), &(DT->D));
    SetMonth(GetMonth(D), &(DT->D));
    SetYear(GetYear(D), &(DT->D));
}

/**
 * Set the time of a given date time
 * @param DT date time
 * @param T time
 */
void SetTime(DateTime *DT, Time T) {
    SetHour(&(DT->T), GetHour(T));
    SetMin(&(DT->T), GetMin(T));
    SetSec(&(DT->T), GetSec(T));
}

/**
 * Returns value wether the inputs are valid to create a date time
 * @param DD day
 * @param MM month
 * @param YY year
 * @param H hour
 * @param M minute
 * @param S second
 * @return is valid
 */
boolean IsValidDateTime(int YY, int MM, int DD, int H, int M, int S) {
    return (IsDateValid(YY, MM, DD) && IsValidTime(H, M, S));
}

/**
 * Creates a date time from the given inputs
 * @param DD day
 * @param MM month
 * @param YY year
 * @param H hour
 * @param M minute
 * @param S second
 * @return a date time
 */
DateTime MakeDateTime(int YY, int MM, int DD, int H, int M, int S) {
    DateTime DT;
    SetDate(&DT, MakeDate(YY, MM, DD));
    SetTime(&DT, MakeTime(H, M, S));
    return DT;
}

/**
 * Returns true if DT1 < DT2
 * @param DT1 first date time
 * @param DT2 second date time
 * @return DT1 < DT2
 */
boolean DateTimeLessThan(DateTime DT1, DateTime DT2) {
    return (DateTimeToSecond(DT1) < DateTimeToSecond(DT2));
}

/**
 * Returns true if DT1 = DT2
 * @param DT1 first date time
 * @param DT2 second date time
 * @return DT1 = DT2
 */
boolean EqDateTime(DateTime DT1, DateTime DT2) {
    return (EqDay(GetDate(DT1), GetDate(DT2)) && EqTime(GetTime(DT1), GetTime(DT2)));
}

/**
 * Returns the amount of seconds of a date time since 1 January 2000
 * @param DT date time
 * @return amount of seconds
 */
long long int DateTimeToSecond(DateTime DT) {
    return (DateToSecond(GetDate(DT)) + TimeToSecond(GetTime(DT)));
}

/**
 * Prints the date time in YYYY-MM-DD HH:MM:SS format
 * @param DT date time
 */
void PrintDateTime(DateTime DT) {
    printf("%04i-%02i-%02i %02i:%02i:%02i", GetYear(DT.D), GetMonth(DT.D), GetDay(DT.D), GetHour(DT.T), GetMin(DT.T), GetSec(DT.T));
}

/**
 * Parse string input with format YYYY-MM-DD HH:MM:SS
 * @param input String input
 * @return Date Time 
 */
DateTime ParseDateTime(CSString input) {
    CSString dateTimeData[100];
    CSString dateData[100];
    CSString timeData[100];
    DateTime dateTimeXX = MakeDateTime(1900, 1, 1, 0, 0, 0);
    int dateTimeDataLength = Split(input, dateTimeData, "@");
    if (dateTimeDataLength >= 2) {
        CSString date = Trim(dateTimeData[0]);
        CSString time = Trim(dateTimeData[1]);
        if (date && time) {
            //parse date
            int dateLength = Split(date, dateData, "-");
            int timeLength = Split(time, timeData, ":");
            if (dateLength >= 3 && timeLength >= 3) {
                dateTimeXX = MakeDateTime(
                        atoi(Trim(dateData[0])),
                        atoi(Trim(dateData[1])),
                        atoi(Trim(dateData[2])),
                        atoi(Trim(timeData[0])),
                        atoi(Trim(timeData[1])),
                        atoi(Trim(timeData[2])));
            }
            Dealocate(date);
            Dealocate(time);
        }
    }
    return dateTimeXX;
}

/**
 * Show date time as string representative
 * @param DT Date Time
 * @return String representative
 */
CSString DateTimeToString(DateTime DT) {
    char *temp;
    temp = (char*) malloc(20 * sizeof (char));
    sprintf(temp, "%04i-%02i-%02i@%02i:%02i:%02i", GetYear(DT.D), GetMonth(DT.D), GetDay(DT.D), GetHour(DT.T), GetMin(DT.T), GetSec(DT.T));
    return temp;
}
